package org.example.warehouse.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.example.warehouse.domain.MallStoreDetail;
import org.example.warehouse.domain.WarehouseStore;
import org.example.warehouse.mapper.MallStoreDetailMapper;
import org.example.warehouse.mapper.WarehouseStoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.warehouse.mapper.OrderDetailMapper;
import org.example.warehouse.domain.OrderDetail;
import org.example.warehouse.service.IOrderDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 订单详情Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-12
 */
@Service
public class OrderDetailServiceImpl implements IOrderDetailService
{
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private MallStoreDetailMapper mallStoreDetailMapper;
    @Autowired
    private WarehouseStoreMapper warehouseStoreMapper;


    /**
     * 查询订单详情
     *
     * @param orderDetailId 订单详情主键
     * @return 订单详情
     */
    @Override
    public OrderDetail selectOrderDetailByOrderDetailId(Long orderDetailId)
    {
        return orderDetailMapper.selectOrderDetailByOrderDetailId(orderDetailId);
    }

    /**
     * 查询订单详情列表
     *
     * @param orderDetail 订单详情
     * @return 订单详情
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<OrderDetail> selectOrderDetailList(OrderDetail orderDetail)
    {

        if(orderDetail.getOrderId() != null){
            return orderDetailMapper.selectOrderDetails(orderDetail);
        }else if( StringUtils.isNotEmpty(orderDetail.getContractId())){
            return orderDetailMapper.selectOrderDetails(orderDetail);
        }
        return orderDetailMapper.selectOrderDetailList(orderDetail);
    }

    /**
     * 新增订单详情
     *
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int insertOrderDetail(OrderDetail orderDetail)
    {
        orderDetail.setCreateTime(DateUtils.getNowDate());
        return orderDetailMapper.insertOrderDetail(orderDetail);
    }

    /**
     * 修改订单详情
     *
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int updateOrderDetail(OrderDetail orderDetail)
    {
        Integer remainNum=orderDetail.getRemainNum()-orderDetail.getOperationNum();
        orderDetail.setRemainNum(remainNum);



        return orderDetailMapper.updateOrderDetail(orderDetail);
    }
    private void editWarehouseStore(MallStoreDetail orderDetail, Date curDate) {
        WarehouseStore store=new WarehouseStore();
        store.setProductId(orderDetail.getProductId());
        store.setWarehouseId(handWarehouseId(orderDetail));
        store.getParams().put("deptId", ShiroUtils.getSysUser().getDeptId());
        WarehouseStore houseStoreDB= warehouseStoreMapper.selectMallWarehouseStoreByProductId(store);
        if(houseStoreDB !=null && houseStoreDB.getWarehouseStoreId()>0){
            //更新库存
            houseStoreDB.setNum(houseStoreDB.getNum()+ orderDetail.getOperationNum());
            houseStoreDB.setRemainNum(handNullValue(houseStoreDB.getRemainNum())+ orderDetail.getOperationNum());
            warehouseStoreMapper.updateWarehouseStore(houseStoreDB);
        }else{
            //新增库存
            store.setNum(orderDetail.getOperationNum());
            store.setRemainNum(orderDetail.getOperationNum());
            store.setWarehouseId(orderDetail.getWarehouseId());
            store.setCreateBy(ShiroUtils.getUserId()+"");
            store.setCreateTime(curDate);
            store.setWarehouseId(handWarehouseId(orderDetail));
            store.setCheckNum(orderDetail.getOperationNum());
            warehouseStoreMapper.insertWarehouseStore(store);
        }
    }

    private Long handWarehouseId(OrderDetail orderDetail){
        if(orderDetail.getWarehouseId()==0L || orderDetail.getWarehouseId()==null){
            return 1L;
        }
        return  orderDetail.getWarehouseId();
    }
    private Integer handNullValue(Integer value){

        if(value==null){
            return 0;
        }
        return value;
    }
    /**
     * 批量删除订单详情
     *
     * @param orderDetailIds 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailByOrderDetailIds(String orderDetailIds)
    {
        return orderDetailMapper.deleteOrderDetailByOrderDetailIds(Convert.toStrArray(orderDetailIds));
    }

    /**
     * 删除订单详情信息
     *
     * @param orderDetailId 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailByOrderDetailId(Long orderDetailId)
    {
        return orderDetailMapper.deleteOrderDetailByOrderDetailId(orderDetailId);
    }
}
